<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<script th:inline="javascript">
[# th:each="fName : ${fieldList}"]
    $(function() {
        var oldValue_[(${fName})] = "cws-65536"; // 一个不存在的值
        if (o("[(${fName})]")) { // 防止此控件也是SQL控件，并且此时还不存在
            oldValue_[(${fName})] = o("[(${fName})]").value;
        }
        setInterval(function(){
            if (o("[(${fName})]")) {
                if (oldValue_[(${fName})] != o("[(${fName})]").value) {
                    oldValue_[(${fName})] = o("[(${fName})]").value;
                    onFormulaCtlRelateFieldChange_[(${fieldName})]();
                }
            }
        },500);
    });
[/]

// 取得本表单中相应的值
function getFieldVal(fieldName) {
    // 先从当前表单中取，如果取不到则从父表单中取
    if (o(fieldName)) {
        return o(fieldName).value;
    }
}

function onFormulaCtlRelateFieldChange_[(${fieldName})]() {
    var params = '[(${args})]'; // 必须得用单引号，因为可能有些参数会是字符串，带有双引号
    var formulaStr = '';
    try {
        formulaStr = "#[(${code})](" + [(${params})] + ")";
    }
    catch (e) {
        console.error(e);
    }

    if (formulaStr.indexOf("undefined") != -1 || formulaStr == "") {
        console.log("onFormulaCtlRelateFieldChange中 [(${params})] 存在JS错误");
    } else {
        $.ajax({
            type: "post",
            contentType:"application/x-www-form-urlencoded; charset=iso8859-1",
            url: getContextPath() + "/visual/formula/doFormula.do",
            async: false,
            data: {
                formula: formulaStr,
                skey: '[(${skey})]'
            },
            dataType: "html",
            beforeSend: function(XMLHttpRequest) {
                // $('#bodyBox').showLoading();
            },
            success: function(data, status) {
                data = $.parseJSON(data);
                if (data.ret=="1") {
                    if (o("[(${fieldName})]")) {
                        $("#[(${fieldName})]").val(data.value);

                        setTimeout(function () {
                            if(o("[(${fieldName})]_show")) {
                                o("[(${fieldName})]_show").innerHTML = data.value;
                            }
                        }, 500);

                        var frm = o("visualForm");
                        if (frm==null) {
                            frm = o("flowForm");
                        }
                    }
                }
                else {
                    mui.alert(data.msg);
                }
            },
            complete: function(XMLHttpRequest, status){
                // $('#bodyBox').hideLoading();
            },
            error: function(XMLHttpRequest, textStatus){
                // 请求出错处理
                alert(XMLHttpRequest.responseText);
            }
        });
    }
}

[# th:if="${editable}"]
$(function() {
    onFormulaCtlRelateFieldChange_[(${fieldName})]();
});
[/]

</script>
</body>
</html>